import type { FormSchemaGetter } from '#/adapter/form';
import type { VxeGridProps } from '#/adapter/vxe-table';

import { DictEnum } from '@vben/constants';

import { productSelect } from '#/api/iot/alarm/config';
import { getDictOptions } from '#/utils/dict';
import { renderDictTags } from '#/utils/render';

export const querySchema: FormSchemaGetter = () => [
  {
    component: 'ApiSelect',
    fieldName: 'productId',
    label: '产品名称',
    componentProps: {
      filterOption: true,
      showSearch: true,
      api: productSelect,
      optionFilterProp: 'productName',
      allowClear: true,
      fieldNames: {
        label: 'productName',
        value: 'id',
      },
    },
  },
];

export const columns: VxeGridProps['columns'] = [
  {
    title: '序号',
    type: 'seq',
    width: 100,
  },
  {
    title: '产品名称',
    field: 'productName',
    showOverflow: true,
  },
  {
    title: '推送消息类型',
    field: 'messageType',

    slots: {
      default: ({ row }) => {
        if (!row.messageType) {
          return '无';
        }
        return renderDictTags(
          row.messageType.split(','),
          getDictOptions(DictEnum.IOT_MESSAGE_TYPE),
          false,
          4,
        );
      },
    },
  },
  {
    title: '创建时间',
    field: 'createTime',
  },
  {
    field: 'action',
    fixed: 'right',
    slots: { default: 'action' },
    title: '操作',
    resizable: false,
    width: 'auto',
  },
];

export const drawerSchema: FormSchemaGetter = () => [
  {
    label: 'id',
    fieldName: 'id',
    component: 'Input',
    dependencies: {
      show: () => false,
      triggerFields: [''],
    },
  },
  {
    component: 'ApiSelect',
    fieldName: 'productId',
    label: '产品名称',
    componentProps: {
      filterOption: true,
      showSearch: true,
      api: productSelect,
      optionFilterProp: 'productName',
      allowClear: true,
      fieldNames: {
        label: 'productName',
        value: 'id',
      },
    },
    dependencies: {
      disabled(values) {
        return !!values.id;
      },
      triggerFields: ['id'],
    },
  },
  {
    component: 'Select',
    componentProps: {
      mode: 'multiple',
      optionFilterProp: 'label',
      options: getDictOptions(DictEnum.IOT_MESSAGE_TYPE),
      allowClear: true,
    },
    fieldName: 'messageType',
    label: '推送消息类型',
    rules: 'selectRequired',
  },
];
